﻿
Imports System.Data.OleDb

Public Class cardetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            If Request.Params("ID") = "" Then Response.Redirect("cardata.aspx")
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CarDataString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM comments WHERE car_fkId=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()
        End If
    End Sub

    Protected Sub btn_AddComment_Click(sender As Object, e As EventArgs) Handles btn_AddComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CarDataString").ConnectionString)
        Dim SqlString As String = "Insert into comments(Author, Comment, Title, car_fkId) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim accountToken As IntPtr = WindowsIdentity.GetCurrent().Token
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_author.Text)
        cmd.Parameters.AddWithValue("@f2", tb_title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        ''''''fill Gridview2
        Dim fillComments As String = "SELECT * FROM comments WHERE car_fkId=@f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()
        tb_author.Text = ""
        tb_comment.Text = ""
        tb_title.Text = ""



    End Sub

    Protected Sub CheckBoxList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckBoxList1.SelectedIndexChanged

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CarDataString").ConnectionString)
        Dim SqlString As String = "INSERT INTO CarTagJunction(Car_fkId,Tag_fkId) VALUES(@f1,@f2)"
        oleDbConn.Open()
        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                'here is where we add the join to the table of tags of cars

                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()

            End If
        Next
        If TextBox1.Text = "" Then Return
        Dim newTags = TextBox1.Text.Split(",")
        For Each newTag In newTags
            Dim newTagSql As String = "INSERT INTO Tags(Tag) VALUES(@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from Tags ORDER BY ID DESC", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim LastId = dr.GetValue(0)

            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            newcmd.Parameters.AddWithValue("@f2", LastId)
            newcmd.ExecuteNonQuery()

        Next

    End Sub

    Protected Sub LoginView1_ViewChanged(sender As Object, e As EventArgs) Handles LoginView1.ViewChanged
        tb_author.Text = User.Identity.Name
    End Sub

    Protected Sub tb_author_TextChanged(sender As Object, e As EventArgs) Handles tb_author.TextChanged

       

    End Sub

    Protected Sub LoginStatus1_LoggingOut(sender As Object, e As LoginCancelEventArgs)



    End Sub
End Class